home *** CD-ROM | disk | FTP | other *** search
/ Aminet 5 / Aminet 5 - March 1995.iso / Aminet / misc / edu / globe099src.lha / Ami-Globe / create_tab_bin.c < prev    next >
C/C++ Source or Header  |  1994-11-17  |  8KB  |  244 lines

  1.         
  2. #include <exec/types.h>
  3. #include <exec/memory.h>
  4. #include <exec/libraries.h>
  5.  
  6. /* Prototypes */
  7. #include <clib/alib_protos.h>
  8. #include <clib/exec_protos.h>
  9. #include <clib/dos_protos.h>
  10. #include <clib/macros.h>
  11.  
  12. /* ANSI C */
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <stdio.h>
  16. #include <ctype.h>
  17. #include <stat.h>
  18. #include <math.h>
  19.  
  20. #define REG(x) register __ ## x
  21. #define ASM    __asm
  22. #define SAVEDS __saveds
  23.  
  24. #include <pragmas/exec_pragmas.h>
  25. #include <pragmas/dos_pragmas.h>
  26.  
  27. #include "amiglobe_types.h"
  28.  
  29.  
  30. #define MIN_RESO        3
  31. CLIP    clip_cur;
  32. CLIP    clip_max;
  33. int     max_border=0;
  34.  
  35. /* Tableau de pointeurs sur les vecteurs */
  36.         
  37. BORDER  *tab_vect;
  38.  
  39. void tstpt(
  40.         int     x,
  41.         int     y,
  42.         int     *pmx,
  43.         int     *pmy,
  44.         int     *pMX,
  45.         int     *pMY
  46.         )
  47. {
  48.         if (x<*pmx)
  49.     {
  50.                 *pmx=x;
  51.         }
  52.         if (x>*pMX)
  53.     {
  54.                 *pMX=x;
  55.         }
  56.         if (y<*pmy)
  57.     {
  58.                 *pmy=y;
  59.         }
  60.         if (y>*pMY)
  61.     {
  62.                 *pMY=y;
  63.         }
  64. }
  65.  
  66. /*********************************************************** read_map() ***/
  67.  
  68. BOOL read_map_bin(
  69.         char *p_filename,
  70.         char *p_out
  71.         )
  72. {
  73.         char    Buffer_Name[100];
  74.         FILE    *input,*fp_out;
  75.         int     mx,my,MX,MY;
  76.         int     i;
  77.  
  78.         int     flg_zone;
  79.         int     flg_end;
  80.         int     num_bord;
  81.         int     Last_Code=0;
  82.         /* Vecteur courant */
  83.         short   int     *tab_temp,*tab_deb;
  84.         char    *p_deb;
  85.         struct  vect *v;
  86.         short   int     nb_vect,nb_vect_small;
  87.         int     nb_read,taille;
  88.         struct  stat    my_stat;
  89.         short   int     minx_real,miny_real,maxx_real,maxy_real;
  90.         BOOL    Flg_Err;
  91.         LONG    pos;
  92.  
  93.         /*printf("Lecture de %s\n",p_filename);*/
  94.         flg_end=FALSE;
  95.         Flg_Err=TRUE;
  96.  
  97.         sprintf(Buffer_Name,"%s.pt_bin",p_filename);
  98.  
  99.         if( (input = fopen(p_filename,"r"))&&(fp_out = fopen(p_out,"w")))
  100.         {
  101.                 /* Premiere chose, la taille max de notre carte...*/
  102.  
  103.                 fread( &minx_real,sizeof(short int),1,input);
  104.                 fwrite(&minx_real,sizeof(short int),1,fp_out);
  105.                 fread( &miny_real,sizeof(short int),1,input);
  106.                 fwrite(&miny_real,sizeof(short int),1,fp_out);
  107.                 fread( &maxx_real,sizeof(short int),1,input);
  108.                 fwrite(&maxx_real,sizeof(short int),1,fp_out);
  109.                 fread( &maxy_real,sizeof(short int),1,input);
  110.                 fwrite(&maxy_real,sizeof(short int),1,fp_out);
  111.                 clip_cur.minx=minx_real;
  112.                 clip_cur.miny=miny_real;
  113.                 clip_cur.maxx=maxx_real;
  114.                 clip_cur.maxy=maxy_real;
  115.                 clip_max=clip_cur;
  116.                 /*printf("minx:%d miny:%d maxx:%d maxy:%d\n",
  117.                         minx_real,miny_real,maxx_real,maxy_real);*/
  118.  
  119.                 stat(p_filename,&my_stat);
  120.                 
  121.                 taille=my_stat.st_size;
  122.                 taille=taille-4*sizeof(short int);
  123.  
  124.                 printf("Taille:%d\n",taille);
  125.  
  126.                 tab_temp=(short int *)malloc(taille);
  127.  
  128.                 tab_deb=tab_temp;
  129.  
  130.                 if (tab_temp==NULL)
  131.                 {
  132.                         printf("ERREUR avec allocation memoire pour le fichier\n");
  133.                 }else{
  134.                    nb_read=fread(tab_temp,1,taille,input);
  135.                    
  136.                    /* lecture du reste du fichier */
  137.                    if (nb_read!=taille)
  138.                    {
  139.                         printf("ERROR: File truncated..\n");
  140.                    }else{
  141.  
  142.                         p_deb=(char *)tab_temp;
  143.  
  144.                         /*printf("Lecture des borders\n");*/
  145.                         num_bord=0;
  146.                         flg_zone=TRUE;
  147.  
  148.                         do
  149.                         {
  150. #ifdef  TOMDEBUG
  151.                                 printf("Vecteur numero:%d\n",num_bord);
  152. #endif
  153.                                 /* Lecture de composantes d'un bord */
  154.                                 mx=100000;
  155.                                 my=100000;
  156.                                 MX=-100000;
  157.                                 MY=-100000;
  158.  
  159.                                 pos=sizeof(short int)*((tab_temp-tab_deb)+4);
  160.                                 nb_vect=*tab_temp++;/* 5eme mot = nb de vecteurs */
  161.                                 tab_vect[num_bord].p_vect=(struct vect *)tab_temp;
  162.                                 tab_temp+=nb_vect*3;
  163.                                 v=tab_vect[num_bord].p_vect;
  164.  
  165.                                 /* On cree la zone de clipping, mais en */
  166.                                 /* excluant les lacs et rivieres        */
  167.                                 for (i=0;i<nb_vect;i++)
  168.                                 {                                       
  169.                                         tstpt(v->x,v->y,&mx,&my,&MX,&MY);
  170.                                         v++;
  171.                                 }
  172.  
  173.                                 v=tab_vect[num_bord].p_vect;                
  174.                                 /* On s'assure que le dernier point est bien de niveau 5 */
  175.                                 if (nb_vect>1)v[nb_vect-1].Code=5;
  176.  
  177.                                 /* On s'assure que le code du premier pt est bien */
  178.                                 /* un indicateur de nouveau segment                       */
  179.  
  180.                                 if (tab_vect[num_bord].p_vect[0].Code<1000)
  181.                                 {
  182.                                         tab_vect[num_bord].p_vect[0].Code=Last_Code;
  183.                                 }
  184.                                 Last_Code=tab_vect[num_bord].p_vect[0].Code;
  185.         
  186.                                 /* Creation du nouveau tableau */
  187.  
  188.                                 nb_vect_small=0;
  189.                                 for(i=0;i<nb_vect;i++){
  190.                                         if(v[i].Code>=MIN_RESO){
  191.                                                 nb_vect_small++;
  192.                                         }
  193.                                 }
  194.  
  195.                                 /* Ecriture de la position ds le fichier origine */
  196.                                 fwrite(&pos,sizeof(LONG),1,fp_out);
  197.                         
  198.                                 /* Ecriture du nveau nb de pt */
  199.                                 fwrite(&nb_vect_small,sizeof(short int),1,fp_out);
  200.  
  201.                                 for(i=0;i<nb_vect;i++){
  202.                                         if(v[i].Code>=MIN_RESO){
  203.         
  204.                                                 fwrite( &v[i],
  205.                                                         sizeof(struct vect),
  206.                                                         1,fp_out);
  207.  
  208.                                         }
  209.                                 }
  210.  
  211.  
  212.                                 if (num_bord>MAX_BORDER)
  213.                                 {
  214.                                         /*printf("ERREUR, depassement nombre de border!\n");*/
  215.                                 }
  216.                                 else
  217.                                 {
  218.                                         num_bord++;
  219.                                 }
  220.                                 if ((char *)tab_temp>=taille+p_deb)
  221.                                 {
  222.                                         flg_zone=FALSE;
  223.                                 }
  224.                         }
  225.                         while(flg_zone==TRUE);
  226.                         max_border=num_bord;
  227.                         Flg_Err=FALSE;
  228.                     }
  229.                 }
  230.                 fclose(fp_out);
  231.         }
  232.         return(Flg_Err);
  233. }
  234.  
  235. main(int argc,char **argv)
  236. {
  237.         tab_vect=(BORDER *)malloc(sizeof(BORDER)*MAX_BORDER);
  238.  
  239.         if(argc>2){
  240.                 read_map_bin(argv[1],argv[2]);
  241.         }
  242. }
  243.  
  244.